<template>
  <el-dialog v-model="visible" width="60%" append-to-body title="选择用户" @close="handleClose">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-suffix=":">
        <el-form-item label="关键字" prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            placeholder="请输入关键字"
            clearable
            @keyup.enter="handleSearch()"
          />
        </el-form-item>
        <el-form-item label="平台" prop="terminal">
          <el-select
            v-model="queryParams.terminal"
            class="!w-[200px]"
            clearable
            placeholder="请选择平台"
          >
            <el-option
              v-for="(item, index) in messageTypeList"
              :value="item.value"
              :label="item.label"
              :key="index"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="search" @click="handleSearch()">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="flex h-[600px]">
      <base-page-table
        ref="tableRef"
        :table-data="pageData"
        :table-columns-data="tableColumnsData"
        :loading="loading"
        :page-num="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        :total="total"
        :height="500"
        row-key="userId"
        @selection-change="handleSelectionChange"
        @handleQuery="handlePageChange"
      >
        <template #avatar="{ row, index }">
          <el-image style="width: 50px; height: 50px" :src="row.avatar" />
        </template>
        <template #userTerminal="{ row, index }">
          {{ getLabelByValue(USER_ROLE_ENUM, row.userTerminal) }}
        </template>
      </base-page-table>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="handleCloseDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import NoticeAPI, { NoticeDetailVO, NoticePageQuery, NoticePageVO } from "@/api/system/notice";
import { getLabelByValue, getLabelValueList } from "@/utils/enums";
import { MESSAGE_TYPE_ENUM, USER_ROLE_ENUM } from "@/enums/notice/NoticeEnum";
const pageData = ref<NoticePageVO[]>([]);
const tableColumnsData = ref([
  { type: "selection", width: 50, align: "center", "reserve-selection": true },
  {
    type: "index",
    label: "序号",
    width: 60,
    align: "center",
  },
  {
    label: "头像",
    width: 150,
    customSlot: "avatar",
    align: "center",
  },
  {
    label: "登录名",
    prop: "username",
    align: "center",
    showOverflowTooltip: true,
  },
  {
    label: "昵称",
    prop: "nickname",
    align: "center",
    showOverflowTooltip: true,
  },
  {
    label: "手机号",
    prop: "mobile",
    align: "center",
    showOverflowTooltip: true,
  },
  {
    label: "平台",
    customSlot: "userTerminal",
    align: "center",
    showOverflowTooltip: true,
  },
]);
const emit = defineEmits(["selectUser"]);
const selections = ref([]);
const selectionIds = ref([]);
const tableRef = ref();
const messageTypeList = getLabelValueList(USER_ROLE_ENUM);
const loading = ref(false);
const visible = ref(false);
const total = ref(0);
const queryFormRef = ref();
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  keywords: "",
  terminal: "",
});
// 行复选框选中项变化
function handleSelectionChange(selection: any) {
  selections.value = selection;
}
function handlePageChange(val) {
  queryParams.pageNum = val.pageNum;
  queryParams.pageSize = val.pageSize;
  handleQuery();
}
// 切换全屏
const handleTabClick = () => {
  handleQuery();
};

// 关闭弹窗
const handleClose = () => {
  visible.value = false;
  tableRef.value.tableRef.clearSelection();
};

// 接收公告详情
const showModal = async (id: string) => {
  visible.value = true;
  selectionIds.value = id;
  handleQuery();
};

function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

function handleSearch() {
  queryParams.pageNum = 1;
  handleQuery();
}

function handleSubmit() {
  visible.value = false;
  emit("selectUser", selections.value);
}
async function handleQuery() {
  try {
    loading.value = true;
    const res = await NoticeAPI.getCustomUsersApi({
      ...queryParams,
    });
    pageData.value = res.list;
    total.value = res.total;
    pageData.value.forEach((item) => {
      const id = selectionIds.value.find((value) => value.id === item.userId);
      if (id) {
        tableRef.value.tableRef.toggleRowSelection(item, true);
      } else {
        tableRef.value.tableRef.toggleRowSelection(item, false);
      }
    });
  } catch (e) {
    console.log(e);
  } finally {
    loading.value = false;
  }
}
// 关闭弹窗
function handleCloseDialog() {
  visible.value = false;
  emit("update:modelValue", false); // 关闭弹窗
}

defineExpose({
  showModal,
});
</script>

<style scoped>
:deep(.el-card__body) {
  box-sizing: border-box;
  height: calc(100% - 50px);
}
</style>
